package cn.huobi.org.service.impl;

import cn.huobi.basic.query.BaseQuery;
import cn.huobi.basic.service.impl.BaseServiceImpl;
import cn.huobi.basic.util.MD5Utils;
import cn.huobi.basic.util.PageList;
import cn.huobi.basic.util.StrUtils;
import cn.huobi.org.domain.Employee;
import cn.huobi.org.mapper.EmployeeMapper;
import cn.huobi.org.service.EmployeeService;
import cn.huobi.user.domain.Logininfo;
import cn.huobi.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Employee> findByDepartmentId(Long departmentId) {
        return employeeMapper.findByDepartmentId(departmentId);
    }

    /**
     * 员工添加
     * @param employee
     */
    public void add(Employee employee){
        // 设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        // MD5加密
        String password = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(password);
        employee.setSalt(salt);

        Logininfo logininfo = this.empConverLogininfo(employee);
        logininfoMapper.add(logininfo);

        employee.setLogininfoId(logininfo.getId());
        employeeMapper.add(employee);

        Long id = employee.getId();
        List<Long> rolesId = employee.getRoles();
        for (Long roleId : rolesId) {
            employeeMapper.addMiddle(id,roleId);
        }
    }

    @Override
    public Employee findByLogininfoId(Long id) {
        return employeeMapper.findByLogininfoId(id);
    }

    @Override
    public List<Employee> findByShopId(Long shopId) {
        return employeeMapper.findByShopId(shopId);
    }

//    /**
//     * 通过Id查询
//     */
//    @Override
//    public Employee findById(Long id) {
//        List<Long> roleIds = roleMapper.findMiddle(id);
//        List<Role> list = new ArrayList<>();
//        for (Long roleId : roleIds) {
//            Role role = roleMapper.findById(roleId);
//            list.add(role);
//        }
//        Employee employee = super.findById(id);
//        employee.setRoles(list);
//        return employee;
//
//    }

    /**
     * 分页查询
     */
    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        PageList<Employee> employeePageList = super.queryByPage(query);
        // 获取到员工的所有数据
        List<Employee> rows = employeePageList.getRows();
        rows.forEach(e->{
            List<Long> roles = employeeMapper.findMiddle(e.getId());
            e.setRoles(roles);
        });
        // 遍历出员工id
//        List<Long> empIds = rows.stream().map(Employee::getId).collect(Collectors.toList());

//        // 根据员工id查询中间表的roleId
//        List<EmployeeRole> roleIds =  employeeMapper.findByMissle(empIds);
//        // 根据员工id分组
//        Map<Long,List<EmployeeRole>> roleIdsMap = roleIds.stream().collect(Collectors.groupingBy(EmployeeRole::getEmployeeId);
//        // 根据roleId查询role表的数据
//        Set<Map.Entry<Long, List<EmployeeRole>>> entries = roleIdsMap.entrySet();
//        for (Map.Entry<Long, List<EmployeeRole>> entry : entries) {
//
//            List<EmployeeRole> value = entry.getValue();
//        }
        /*
        @Override
        public PageList<Employee> queryByPage(BaseQuery query) {
            PageList<Employee> pageList = super.queryByPage(query);
            List<Employee> emps = pageList.getRows();
            emps.forEach(e->{
                List<Long> roles = roleMapper.findRoles(e.getId());
                e.setRoles(roles);
            });
            return pageList;
}

         */

        return employeePageList;
   }


    @Override
    public void delete(Long id) {
        Employee employee = employeeMapper.findById(id);
        logininfoMapper.delete(employee.getLogininfoId());
        employeeMapper.deleteMiddle(id);
        super.delete(id);
    }

    /**
     * 将 emp 转换为 logininfo
     */
    private Logininfo empConverLogininfo(Employee employee){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(1);
        return logininfo;
    }

    /**
     * 员工修改
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        Long id = employee.getId();
        employeeMapper.deleteMiddle(id);
        // 重新添加新的中间表
        List<Long> roles = employee.getRoles();
        for (Long role : roles) {
            employeeMapper.addMiddle(employee.getId(),role);
        }
        super.update(employee);
    }

}
